﻿using log4net;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Suk.Interface;
using Suk.Models;

namespace Suk.Web.Areas.Admin
{
    public class SUBaseController<IService, TEntity> : Controller
        where TEntity : BaseModel,new ()
        where IService : IBaseInterface<TEntity>
    {
        public IService _IService { get; set; }
        public ILog _log{ get; set; }

        public virtual IActionResult Index()
        {
            return View();
        }

        public virtual JsonResult List(TEntity model,int page=1, int limit=10)
        {

            int total = 0;
            var getAll = _IService.FindEntity(model,ref total, page, limit);

            var data = new { code = 0, msg = "", count = total, data = getAll };
            return Json(data);
        }


        public virtual JsonResult Delete(List<TEntity> model)
        {
            int data = 0;

            data = _IService.Delete(model);


            return Json(data);
        }

        public virtual JsonResult Add(TEntity model)
        {
            int data = 0;
            model.CreateTime = System.DateTime.Now;
            if (model.Id == 0)
            {
                data = _IService.Insert(model);
            }
            else
            {
                data = _IService.Update(model);
            }

            return Json(data);
        }

    }
}
